 package com.qx.frame.panel;

import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import org.dom4j.Document;
import org.dom4j.Element;

import com.qx.iconst.PathConst;
import com.qx.util.AppFunction;
import com.qx.util.SwingUtils;

/**
 * 描述：<br>
 * 信息模块
 * 
 * @author LazyBear
 * @data 2014-4-22
 */
public class InfoPanel extends JPanel {
	private static final long serialVersionUID = 2237645206774947745L;

	private static InfoPanel instance;

	/**
	 * xml读取对象
	 */
	private static Document document;

	public static InfoPanel getInfoPanel() {
		if (instance == null)
			instance = new InfoPanel();
		return instance;
	}

	private InfoPanel() {
		try {
			document = AppFunction.readXml(PathConst.users_cfg);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SwingUtils.initPanel(this);
		initTable();
		this.setLayout(null);
		this.setVisible(false);
	}

	/**
	 * 初始化表格
	 * 
	 * @author LazyBear 描述:<br>
	 */
	private void initTable() {
		List<Element> elements = document.getRootElement().elements();
		if (elements.size() < 1) {
			System.err.println("no users!can't test.....");
			System.exit(0);
		}
		String[] colNames = new String[elements.get(0).attributes().size()];
		for (int i = 0; i < colNames.length; i++)
			colNames[i] = elements.get(0).attribute(i).getName();
		Object[][] data = new Object[elements.size()][elements.get(0).attributes().size()];
		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < data[i].length; j++) {
				data[i][j] = elements.get(i).attribute(j).getValue();
			}
		}
		JTable t = new JTable(data, colNames) {
			private static final long serialVersionUID = 8256572034028811298L;
			public boolean isCellEditable(int row, int column) {
				return false;

			}
		};
		t.getTableHeader().setReorderingAllowed(false);
		JPanel p = new JPanel();
		JScrollPane s = new JScrollPane(t);
		p.add(s);
		p.setBounds(5, 5, 480, 460);
		this.add(p);
	}
}
